package com.example.pushdeom.Bean;

import android.content.Context;
import android.widget.Toast;

import com.tencent.connect.UserInfo;
import com.tencent.open.utils.HttpUtils;
import com.tencent.tauth.IRequestListener;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;

public class BaseUiListener implements IUiListener {
   private Context mContext;
   private String mScope;
   private Tencent mTencent;
    public BaseUiListener() {
    }

    public BaseUiListener(Context mContext) {
        super();
        this.mContext = mContext;
    }

    public BaseUiListener(Context mContext, String mScope,Tencent mTencent) {
        super();
        this.mContext = mContext;
        this.mScope = mScope;
        this.mTencent = mTencent;
    }


    @Override
    public void onComplete(Object o) {
        JSONObject jsonObject = (JSONObject) o;

        String openid = null;
        try {
            openid = jsonObject.getString("openid");
            String Access_token = jsonObject.getString("access_token");
            Integer expires_in = jsonObject.getInt("expires_in");
            //这里url没有用到，这是请求API接口requestAsync
            String url = "https://graph.qq.com/user/get_user_info?access_token="
                    + Access_token
                    + "&"
                    + "oauth_consumer_key=101919484"
                    + "&"
                    + "openid=" + openid;
            AppSharedPreferences.setPreference(mContext,"openid",openid);
            mTencent.setOpenId(openid);
            mTencent.setAccessToken(Access_token, expires_in+"");
            UserInfo qqInfo = new UserInfo(mContext,mTencent.getQQToken());
            qqInfo.getUserInfo(getQQinfoListener);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Toast.makeText(mContext, "登录成功", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onError(UiError uiError) {
        Toast.makeText(mContext, "登录失败", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onCancel() {
        Toast.makeText(mContext, "登录取消", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onWarning(int i) {
        Toast.makeText(mContext, "登录报错", Toast.LENGTH_LONG).show();
    }

    private IUiListener getQQinfoListener = new IUiListener() {

        @Override
        public void onComplete(Object o) {
            JSONObject jsonObject1 = (JSONObject) o;
            try {
                String userName = jsonObject1.getString("nickname");
                String gender = jsonObject1.getString("gender");
                String userImg = jsonObject1.getString("figureurl_2");//100大小
                AppSharedPreferences.setPreference(mContext,"nickname",userName);
                AppSharedPreferences.setPreference(mContext,"gender",gender);
                AppSharedPreferences.setPreference(mContext,"userImg",userImg);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onError(UiError uiError) {

        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onWarning(int i) {

        }
    };
}
